/**
 * 
 */
package FileStatus;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Properties;
import java.util.Scanner;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import Config.GeneralsConfig;

/**
 * 读取配置文件，对全局变量设置
 *
 * @create 2014.11.01
 * @latest 2014.11.03
 *
 * @author sundaydx
 * @since 2.0.1
 * @version 2.0.1
 *
 */
public class Property {

	/**
	 * 配置文件初始化变量 properties 配置文件读取 obj 全局配置的Class对象
	 */
	private static Properties properties;
	private static Class<?> obj;

	private final static Logger logger = LogManager.getLogger(Property.class
			.getName());

	public static void readProperty(String Mac) throws ClassNotFoundException,
			FileNotFoundException, IOException, NumberFormatException,
			NoSuchFieldException, SecurityException, IllegalArgumentException,
			IllegalAccessException {
		File fileProperty = new File(Mac + "/pro.ini");

		/** 全局变量赋值 */
		GeneralsConfig.intSecond4Wait = 60;
		GeneralsConfig.intSecond4NextTask = 60;
		GeneralsConfig.intRows2Dump = 100000;
		GeneralsConfig.intSecond4SQLWait = 120;
		GeneralsConfig.stringDataBaseDriver = "";
		GeneralsConfig.stringDataBaseUrl = "";
		GeneralsConfig.stringDataBaseUsername = "";
		GeneralsConfig.stringDataBasePassword = "";

		if (fileProperty.exists()) {
			obj = Class.forName("Config.GeneralsConfig");

			Scanner input = new Scanner(fileProperty);
			setGenerals(input);
		}
	}

	public static void writeProperty(String Mac) throws IOException {
		File fileProperty = new File(Mac + "/pro.ini");

		FileWriter fw = new FileWriter(fileProperty, false);

		fw.write("intSecond4Wait=" + GeneralsConfig.intSecond4Wait + "\n");
		fw.write("intSecond4NextTask=" + GeneralsConfig.intSecond4NextTask
				+ "\n");
		fw.write("intRows2Dump=" + GeneralsConfig.intRows2Dump + "\n");
		fw.write("intSecond4SQLWait=" + GeneralsConfig.intSecond4SQLWait + "\n");
		fw.write("stringDataBaseDriver=" + GeneralsConfig.stringDataBaseDriver
				+ "\n");
		fw.write("stringDataBaseUrl=" + GeneralsConfig.stringDataBaseUrl + "\n");
		fw.write("stringDataBaseUsername="
				+ GeneralsConfig.stringDataBaseUsername + "\n");
		fw.write("stringDataBasePassword="
				+ GeneralsConfig.stringDataBasePassword + "\n");

		fw.close();

	}

	/**
	 * 批量设置全局变量
	 */
	private static void setGenerals(Scanner input)
			throws NumberFormatException, NoSuchFieldException,
			SecurityException, IllegalArgumentException, IllegalAccessException {

		while (input.hasNext()) {
			String context = input.nextLine();
			String[] args = context.split("=");
			if (args.length == 2) {
				setValue(args[0], args[1].replace("\n", ""));
				System.out.println(args[0] + "  " + args[1].replace("\n", ""));
			}
		}
	}

	/**
	 * 根据变量名进行读取赋值
	 *
	 * @param keyName
	 *            进行设置的全局变量名
	 *
	 * @throws NoSuchFieldException
	 *             变量不存在
	 *
	 * @throws SecurityException
	 *             越界访问
	 *
	 * @throws NumberFormatException
	 *             错误类型覆盖
	 *
	 * @throws IllegalArgumentException
	 *             变量类型错误设置
	 *
	 * @throws IllegalAccessException
	 *             变量非法访问
	 *
	 * @create 2014.11.01
	 * @latest 2014.11.01
	 *
	 * @author sundaydx
	 * @since 2.0.1
	 * @version 2.0.1
	 */
	private static void setValue(String keyName, String keyValue)
			throws NoSuchFieldException, SecurityException,
			NumberFormatException, IllegalArgumentException,
			IllegalAccessException {

		/** 通过变量名获取对应变量 */
		Field key = obj.getDeclaredField(keyName);

		/** 判断类型，进行赋值 */
		if (key.getType().getName().equalsIgnoreCase("int")) {
			key.set(obj, Integer.valueOf(keyValue));
		} else {
			key.set(obj, keyValue);
		}
	}

}
